using System.Windows;
using System.Windows.Threading;

namespace LicenseApp.Client;

/// <summary>
/// 非阻塞提醒窗口
/// </summary>
public partial class NotificationWindow : Window
{
    private readonly DispatcherTimer _autoCloseTimer;
    private readonly int _autoCloseSeconds;

    public NotificationWindow(string message, LicenseStatus status = LicenseStatus.Valid, int autoCloseSeconds = 5)
    {
        InitializeComponent();
        
        _autoCloseSeconds = autoCloseSeconds;
        
        // 设置消息内容
        txtMessage.Text = message;
        
        // 根据状态设置样式
        SetNotificationStyle(status);
        
        // 设置窗口位置（右下角）
        SetWindowPosition();
        
        // 设置自动关闭定时器
        _autoCloseTimer = new DispatcherTimer();
        _autoCloseTimer.Interval = TimeSpan.FromSeconds(autoCloseSeconds);
        _autoCloseTimer.Tick += AutoCloseTimer_Tick;
        _autoCloseTimer.Start();
        
        // 窗口加载完成后开始淡入动画
        Loaded += NotificationWindow_Loaded;
    }

    private void SetNotificationStyle(LicenseStatus status)
    {
        switch (status)
        {
            case LicenseStatus.Valid:
                txtIcon.Text = "✅";
                txtTitle.Text = "授权信息";
                break;
                
            case LicenseStatus.ExpiringSoon:
                txtIcon.Text = "⚠️";
                txtTitle.Text = "授权提醒";
                break;
                
            case LicenseStatus.ExpiredGracePeriod:
                txtIcon.Text = "🕐";
                txtTitle.Text = "授权到期";
                break;
                
            case LicenseStatus.Expired:
                txtIcon.Text = "❌";
                txtTitle.Text = "授权过期";
                break;
        }
    }

    private void SetWindowPosition()
    {
        // 获取屏幕工作区域
        var workingArea = SystemParameters.WorkArea;
        
        // 设置窗口位置到右下角
        Left = workingArea.Right - Width - 20;
        Top = workingArea.Bottom - Height - 20;
    }

    private void NotificationWindow_Loaded(object sender, RoutedEventArgs e)
    {
        // 淡入动画
        Opacity = 0;
        var fadeInAnimation = new System.Windows.Media.Animation.DoubleAnimation(0, 1, TimeSpan.FromMilliseconds(300));
        BeginAnimation(OpacityProperty, fadeInAnimation);
    }

    private void AutoCloseTimer_Tick(object? sender, EventArgs e)
    {
        _autoCloseTimer.Stop();
        CloseWithAnimation();
    }

    private void BtnClose_Click(object sender, RoutedEventArgs e)
    {
        _autoCloseTimer.Stop();
        CloseWithAnimation();
    }

    private void BtnOK_Click(object sender, RoutedEventArgs e)
    {
        _autoCloseTimer.Stop();
        CloseWithAnimation();
    }

    private void CloseWithAnimation()
    {
        // 淡出动画
        var fadeOutAnimation = new System.Windows.Media.Animation.DoubleAnimation(1, 0, TimeSpan.FromMilliseconds(200));
        fadeOutAnimation.Completed += (s, e) => Close();
        BeginAnimation(OpacityProperty, fadeOutAnimation);
    }

    /// <summary>
    /// 显示非阻塞通知
    /// </summary>
    /// <param name="message">消息内容</param>
    /// <param name="status">授权状态</param>
    /// <param name="autoCloseSeconds">自动关闭秒数</param>
    public static void ShowNotification(string message, LicenseStatus status = LicenseStatus.Valid, int autoCloseSeconds = 5)
    {
        try
        {
            // 确保在UI线程中执行
            if (Application.Current?.Dispatcher.CheckAccess() == true)
            {
                var notification = new NotificationWindow(message, status, autoCloseSeconds);
                notification.Show();
            }
            else
            {
                Application.Current?.Dispatcher.BeginInvoke(() =>
                {
                    var notification = new NotificationWindow(message, status, autoCloseSeconds);
                    notification.Show();
                });
            }
        }
        catch
        {
            // 如果无法显示窗口，忽略错误（可能是控制台应用等）
        }
    }
}
